﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Jamhee.Noctiluca.LightWiki.Markup
{
    class List : WikiMarkup
    {
        private const string LIST = @"^\s*(\*+|#+) (.*)";

        protected override string ConvertMarkup(string text, string docFolder, Uri url, MarkupStack stack, System.Text.RegularExpressions.Regex regex)
        {
            string html = text;
            MatchCollection matches = regex.Matches(html);
            foreach (Match match in matches)
            {
                if (match.Groups.Count == 3)
                {
                    // Get the values from content
                    string mark = match.Groups[1].Value;
                    string itemText = match.Groups[2].Value;

                    // Check the list type
                    string html_list_start = "";
                    string html_list_end = "";
                    ListStackFlag currentStack = null;

                    if (mark.StartsWith("*"))
                    {
                        html_list_start = "<ul>";
                        html_list_end = "</ul>";
                        currentStack = new ListStackFlag(ListStackFlag.ListType.UNORDERED, mark.Length);
                    }
                    else if (mark.StartsWith("#"))
                    {
                        html_list_start = "<ol>";
                        html_list_end = "</ol>";
                        currentStack = new ListStackFlag(ListStackFlag.ListType.ORDERED, mark.Length);
                    }

                    // Compare with last stack flag
                    IMarkupStackFlag lastStack = stack.Pop();
                    if (lastStack == null || !(lastStack is ListStackFlag))
                    {
                        html = regex.Replace(html, html_list_start + "<li>" + itemText);
                        stack.Push(currentStack);
                    }
                    else
                    {
                        switch (currentStack.CompareTo((ListStackFlag)lastStack))
                        {
                            case 0:
                                html = regex.Replace(html, "<li>" + itemText + "</li>");

                                // Push last stacks
                                stack.Push(lastStack);
                                break;
                            case 1:
                                html = regex.Replace(html, html_list_start + "<li>" + itemText + "</li>");

                                // Push last and current stacks
                                stack.Push(lastStack);
                                stack.Push(currentStack);
                                break;
                            case -1:
                                string prefix = "";
                                for (int xx = 0; xx < ((ListStackFlag)lastStack).Level - currentStack.Level; xx++)
                                {
                                    prefix += html_list_end;
                                }

                                html = regex.Replace(html, prefix + "<li>" + itemText + "</li>");

                                // Push current stack
                                stack.Push(currentStack);
                                break;
                        }
                    }
                }
            }

            return html;
        }

        public override bool AddParagraphMarkup
        {
            get { return false; }
        }

        protected override string  MarkupRegEx
        {
	        get { return LIST; }
        }
    }
}
